<template>
  <div class="login-container">
    <h2 class="login-title">欢迎登录</h2>
    <form @submit.prevent="login" class="login-form">
      <div class="input-group">
        <label for="username" class="input-label">用户名</label>
        <input type="text" v-model="username" id="username" placeholder="请输入用户名" class="input-field" required/>
      </div>
      <div class="input-group">
        <label for="password" class="input-label">密码</label>
        <input type="password" v-model="password" id="password" placeholder="请输入密码" class="input-field" required/>
      </div>
      <button type="submit" class="login-button">登录</button>
    </form>
    <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
  </div>
</template>

<script>

import {
  loginUser
} from '/src/api/newsTop'

export default {
  data() {
    return {
      username: '',
      password: '',
      errorMessage: ''
    };
  },
  methods: {
    login() {
      // 这里替换为你的后台接口
      loginUser({
        userName: this.username,
        passWord: this.password
      }).then(response => {
        if (response.data.success) {
          localStorage.setItem('token', response.data.data.token);
          this.$router.push('/'); // 登录成功后跳转到系统页面
        } else {
          this.errorMessage = '登录失败，请检查用户名和密码';
        }
      })
    }
  }
}
;
</script>

<style scoped>
/* 整体登录容器 */
.login-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: #f7f7f7;
}

.login-title {
  font-size: 24px;
  margin-bottom: 20px;
  color: #333;
}

.login-form {
  width: 100%;
  max-width: 400px;
  background: #fff;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

.input-group {
  margin-bottom: 20px;
}

.input-label {
  font-size: 14px;
  color: #666;
  margin-bottom: 8px;
  display: block;
}

.input-field {
  width: 100%;
  padding: 10px;
  font-size: 16px;
  border: 1px solid #ccc;
  border-radius: 5px;
  outline: none;
  transition: border-color 0.3s;
}

.input-field:focus {
  border-color: #3498db;
}

.login-button {
  width: 100%;
  padding: 12px;
  background-color: #3498db;
  color: #fff;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.login-button:hover {
  background-color: #2980b9;
}

.error-message {
  color: red;
  font-size: 14px;
  text-align: center;
  margin-top: 10px;
}
</style>
